package br.com.formulavendas.businessusercase;
// default package

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.LockOptions;
import org.hibernate.Query;
import org.hibernate.criterion.Example;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import br.com.formulavendas.domainmodel.Usuario;

/**
 	* A data access object (DAO) providing persistence and search support for Usuario entities.
 			* Transaction control of the save(), update() and delete() operations 
		can directly support Spring container-managed transactions or they can be augmented	to handle user-managed Spring transactions. 
		Each of these methods provides additional information for how to configure it for the desired type of transaction control. 	
	 * @see .Usuario
  * @author MyEclipse Persistence Tools 
 */
public class ManterUsuario extends BaseBusinessUserCase  
		implements br.com.formulavendas.businessusercase.baseinterface.IBasePersistence<Usuario>,
		br.com.formulavendas.businessusercase.baseinterface.IBaseFind<Usuario> {
	
	     private static final Logger log = LoggerFactory.getLogger(ManterUsuario.class);
	
	     //property constants
	public static final String LOGIN = "login";
	public static final String SENHA = "senha";

	public Usuario autenticar(String login, String senha)
	{
		Usuario usuarioAux = this.findByLogin(login);
		
		if (usuarioAux != null) 
			{
				if (usuarioAux.getSenha().equals(senha))
					return usuarioAux;
				else return null;
			}		
		else 
			{
				return null;
			}
	}
	
    
    public void save(Usuario transientInstance) {
        log.debug("saving Usuario instance");
        try {
            this._session.save(transientInstance);
            log.debug("save successful");
        } catch (RuntimeException re) {
            log.error("save failed", re);
            throw re;
        }
    }
    
	public void delete(Usuario persistentInstance) {
        log.debug("deleting Usuario instance");
        try {
            this._session.delete(persistentInstance);
            log.debug("delete successful");
        } catch (RuntimeException re) {
            log.error("delete failed", re);
            throw re;
        }
    }
    
    public Usuario findById( java.lang.Integer id) {
        log.debug("getting Usuario instance with id: " + id);
        try {
            Usuario instance = (Usuario) this._session
                    .get("Usuario", id);
            return instance;
        } catch (RuntimeException re) {
            log.error("get failed", re);
            throw re;
        }
    }
    
    
    public List findByExample(Usuario instance) {
        log.debug("finding Usuario instance by example");
        try {
            List results = this._session
                    .createCriteria("Usuario")
                    .add(Example.create(instance))
            .list();
            log.debug("find by example successful, result size: " + results.size());
            return results;
        } catch (RuntimeException re) {
            log.error("find by example failed", re);
            throw re;
        }
    }    
    
    public List findByProperty(String propertyName, Object value) {
      log.debug("finding Usuario instance with property: " + propertyName
            + ", value: " + value);
      try {
         String queryString = "from Usuario as model where model." 
         						+ propertyName + "= ?";
         Query queryObject = this._session.createQuery(queryString);
		 queryObject.setParameter(0, value);
		 return queryObject.list();
      } catch (RuntimeException re) {
         log.error("find by property name failed", re);
         throw re;
      }
	}

	public Usuario findByLogin(Object login) {
		
		String query = "from Usuario as u where u.login = ?";		
		Query queryObj = this._session.createQuery(query);
		queryObj.setParameter("login", login);		
		
		return (Usuario)queryObj.uniqueResult();
	}
	

	public List findAll() {
		log.debug("finding all Usuario instances");
		try {
			String queryString = "from Usuario";
	         Query queryObject = this._session.createQuery(queryString);
			 return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}
	
    public Usuario merge(Usuario detachedInstance) {
        log.debug("merging Usuario instance");
        try {
            Usuario result = (Usuario) this._session
                    .merge(detachedInstance);
            log.debug("merge successful");
            return result;
        } catch (RuntimeException re) {
            log.error("merge failed", re);
            throw re;
        }
    }

    public void attachDirty(Usuario instance) {
        log.debug("attaching dirty Usuario instance");
        try {
            this._session.saveOrUpdate(instance);
            log.debug("attach successful");
        } catch (RuntimeException re) {
            log.error("attach failed", re);
            throw re;
        }
    }
    
    public void attachClean(Usuario instance) {
        log.debug("attaching clean Usuario instance");
        try {
                      	this._session.buildLockRequest(LockOptions.NONE).lock(instance);
          	            log.debug("attach successful");
        } catch (RuntimeException re) {
            log.error("attach failed", re);
            throw re;
        }
    }
}